﻿

var zsgc;

function oneok(obj) {
    zsgc = obj;
}

//添加窗口
function add() {
    $('#addModal').modal({
        keyboard: true,
        remote: true,
    });
}

//添加数据
function addajax(obj) {
    $(obj).button('loading');
    $.ajax({
        url: zsgc.addurl,
        type: zsgc.addtype,
        data: $('#addform').serialize(),
        success: function (data) {
            $(obj).button('reset');
            $('#addModal').modal('hide');
            //信息提示
            $.globalMessenger().post({
                message: "成功添加",
                hideAfter: 3,
                //type: 'error'
            });
        }
    });
}
//修改数据
function editajax(obj) {
    $(obj).button('loading');
    $.ajax({
        url: '@Url.Action("Edit")',
        type: 'get',
        data: $('#editform').serialize(),
        success: function (data) {
            $(obj).button('reset');
            $('#editModal').modal('hide');
            //信息提示
            $.globalMessenger().post({
                message: "修改成功",
                hideAfter: 3,
                //type: 'error'
            });
        }
    });
}

function Kvm() {

    var k = this;
    //数据集
    k.datas = ko.observableArray();
    //分页数字数组
    k.total = ko.observableArray();
    //修改模型
    k.editmodel = ko.observable({
        Name: '',
        Id: '',
        ts: ''
    });
    //修改
    k.edit = function ($model) {
        $.get('@Url.Action("Edit", "User")' + '?id=' + $model.Id, function (data) {
            k.editmodel(data);
            $('#editModal').modal({
                keyboard: false,
                remote: true,
            });
        });

    };
    //分页
    k.sel = function ($page) {
        fy($page);
    };
}

var kvm = new Kvm();

//获取数据
function fy($page) {
    $.ajax({
        url: '@Url.Action("List", "User")',
        type: 'post',
        //page：第几页，rows：每页几个
        data: { page: $page, rows: 9, },
        dataType: 'json',

        success: function (data) {
            kvm.datas(data);
            kvm.total(data.Viewpages);

            //kvm.maxp(data.MaxPages);
            //kvm.minp(data.MinPages);
        },

    });
}


var sousuodata;
$(function () {
    //即时搜索
    $('#sousuo').typeahead({

        source: function (query, process) {
            $.get('@Url.Action("Sousuo")' + '?wd=' + query, function (d) {
                sousuodata = d;
                var arr = new Array();
                for (var i = 0; i < sousuodata.length; i++) {
                    arr.push(sousuodata[i].Name);
                }

                process(arr);
            });
        },
        highlighter: function (item) {
            var obj = new Object();
            for (var i = 0; i < sousuodata.length; i++) {
                if (sousuodata[i].Name == item) {
                    obj = sousuodata[i];
                }
            }
            return obj.Id + "<==>" + obj.Name + "<==>";
        },
        updater: function (item) {
            //var obj = new Object();
            //for (var i = 0; i < sousuodata.length; i++) {
            //    if (sousuodata[i].Name == item) {
            //        obj = sousuodata[i];
            //    }
            //}

            return item;
        }

    });
    //绑定
    ko.applyBindings(kvm);
    //获取数据
    fy(1);

});